package com.sky.service.impl;

import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //1、将要传递的参数使用map双列集合进行封装
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //2、根据日期查询用户表，获取新增用户数
        Integer newUsers = userMapper.getUserCount(map);
        //3、根据日期查询订单表，获取总订单数,总订单数不需要管订单的状态
        Integer totalOrderCount = orderMapper.getOrderCount(map);
        //4、根据日期查询订单表，获取有效订单数，有效订单数只管订单的状态为5
        map.put("status",5);
        Integer validOrderCount = orderMapper.getOrderCount(map);
        //5、根据总订单数和有效订单数可以计算出订单完成率
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            orderCompletionRate = validOrderCount.doubleValue()/totalOrderCount.doubleValue();
        }
        //6、根据日期查询订单表，获取营业额，营业额只管订单的状态为5
        Double turnover = orderMapper.sumByMap(map);
        if(turnover == null){
            turnover = 0.0;
        }
        //7、根据日期查询订单表，获取平均客单价:营业额除有效订单数，平均客单价只管订单的状态为5
        Double unitPrice = 0.0;
        if(validOrderCount != 0){
            unitPrice = turnover/validOrderCount;
        }
        //8、将获取到的数据封装到BusinessDataVO对象中
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
        return businessDataVO;
    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        //先根据不同的状态进行查询已起售套餐数量和已停售套餐的数据，然后进行数据的封装返回数据
        Integer status = 1;
        //1、根据日期查询已起售套餐数量
        Integer sold = setmealMapper.getSetmealCount(status);
        //2、根据日期查询已停售套餐数量
        status = 0;
        Integer discontinued = setmealMapper.getSetmealCount(status);
        //3、将获取到的数据封装到SetmealOverViewVO对象中
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return setmealOverViewVO;
    }

    @Override
    public SetmealOverViewVO getOverviewDishes() {
        //先根据不同的状态进行查询已起售菜品数量和已停售菜品的数据，然后进行数据的封装返回数据
        Integer status = 1;
        Integer sold = dishMapper.getDishCount(status);
        status = 0;
        Integer discontinued = dishMapper.getDishCount(status);
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return setmealOverViewVO;
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {
        //获取全部订单数据，已取消数量、已完成数量、待派送数量、待接单数量
        //1、获取全部订单数据
        Integer allOrders = orderMapper.getOrderCount(null);
        Map map = new HashMap();
        map.put("status",6);
        //2、获取已取消数量
        Integer cancelledOrders = orderMapper.getOrderCount(map);
        //3、获取已完成数量
        map.put("status",5);
        Integer completedOrders = orderMapper.getOrderCount(map);
        //4、获取待派送数量
        map.put("status",3);
        Integer deliveredOrders = orderMapper.getOrderCount(map);
        //5、获取待接单数量
        map.put("status",2);
        Integer waitingOrders = orderMapper.getOrderCount(map);
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
        return orderOverViewVO;
    }
}
